package com.vehicle_information_management_system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle_information_management_system.dto.TripCount;
import com.vehicle_information_management_system.dto.TripDto;
import com.vehicle_information_management_system.mapper.VehicleInfoMapper;
import com.vehicle_information_management_system.pojo.TripInfo;
import com.vehicle_information_management_system.service.TripInfoService;
import com.vehicle_information_management_system.mapper.TripInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;

/**
* @author huawei
* @description 针对表【trip_info】的数据库操作Service实现
* @createDate 2025-02-17 10:08:02
*/
@Service
@Slf4j
public class TripInfoServiceImpl extends ServiceImpl<TripInfoMapper, TripInfo>
    implements TripInfoService{
    @Autowired
    private TripInfoMapper tripInfoMapper;
    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;

    @Override
    public IPage<TripInfo> getTripList(Integer pageNum, Integer pageSize, String search) {
        Page<TripInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<TripInfo> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(search)){
            queryWrapper.like("v.license_plate",search).or().like("d.name",search)
                    .or().like("t.usage_time",search).or().like("t.return_time",search)
                    .or().like("u.username",search);;
        }
        return tripInfoMapper.getTripList(page,queryWrapper);
    }

    @Override
    public void update(TripInfo tripInfo) {
        tripInfoMapper.updateTripInfo(tripInfo);
        TripDto tripDto =tripInfoMapper.getTripInfoByTripId(tripInfo.getTripId());
        log.info("vehicleId:{}",tripDto.getVehicleId());
        vehicleInfoMapper.updateUseStatus(tripDto.getVehicleId(),1);
    }

    @Override
    public void add(TripInfo tripInfo) {
        tripInfoMapper.insert(tripInfo);
    }

    @Override
    public void delete(Integer id) {
        tripInfoMapper.deleteById(id);
    }

    @Override
    public Integer getTripCountByMonth() {

        QueryWrapper<TripInfo> queryWrapper = new QueryWrapper<>();
        LocalDate now = LocalDate.now();
        YearMonth yearMonth = YearMonth.from(now);
        queryWrapper.apply("YEAR(usage_time) = {0} AND MONTH(usage_time) = {1}", yearMonth.getYear(), yearMonth.getMonthValue());
        return Math.toIntExact(count(queryWrapper));
    }

    @Override
    public IPage<TripDto> getTripListByUserId(Integer pageNum, Integer pageSize, Integer userId) {
        Page<TripDto> page=new Page<>(pageNum,pageSize);
        QueryWrapper<TripDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return tripInfoMapper.getTripListByUserId(page,queryWrapper);

    }

    @Override
    public List<TripCount> getTripCountByDepartment() {
        return tripInfoMapper.getTripCountByDepartment();
    }
}




